package com.linkhub.oms.admin.modules.stock.job;

import cn.hutool.core.collection.CollUtil;
import com.linkhub.oms.admin.common.enums.NeedCheckDuplicatedBiz;
import com.linkhub.oms.admin.modules.stock.service.StockHandler;
import com.linkhub.oms.admin.modules.wms.domain.dto.*;
import com.linkhub.oms.common.util.json.QtFastjsonUtil;
import com.linkhub.oms.component.trace.util.MDCUtil;
import com.linkhub.oms.dao.mpservice.IDuplicatedRecordService;
import com.linkhub.oms.entity.DuplicatedRecordEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName CompensationStockJob
 * @Description TODO
 * @Author XieYu
 * @Date 2024/7/18 22:04
 * @Version 1.0
 */
@Slf4j
@Component
public class CompensationStockJob {
    @Resource
    private IDuplicatedRecordService duplicatedRecordService;
    @Resource
    private StockHandler stockHandler;

    @Scheduled(cron = "0 0/10 * * * ?")
    public void compensationStock() {
        MDCUtil.setTraceAndRequestId();
        log.info("库存管理-补偿任务-开始执行");
        List<DuplicatedRecordEntity> duplicatedRecordEntityList = duplicatedRecordService.listUnfinished();
        if (CollUtil.isEmpty(duplicatedRecordEntityList)) {
            log.info("库存管理-补偿任务-需要补偿的记录");
            return;
        }

        int success = 0;
        for (DuplicatedRecordEntity duplicatedRecordEntity : duplicatedRecordEntityList) {
            NeedCheckDuplicatedBiz needCheckDuplicatedBiz = NeedCheckDuplicatedBiz.valueOf(duplicatedRecordEntity.getType());
            switch (needCheckDuplicatedBiz) {
                case WMS_INBOUND_CONFIRM:
                    stockHandler.wmsInboundConfirm(QtFastjsonUtil.parse(duplicatedRecordEntity.getData(), WarehouseArrantConfirmDTO.class));
                    break;
                case WMS_OUTBOUND_CONFIRM:
                    stockHandler.wmsOutboundConfirm(QtFastjsonUtil.parse(duplicatedRecordEntity.getData(), DeliveryListConfirmDTO.class));
                    break;
                case WMS_COMBINE_PROCESSING_CONFIRM:
                    stockHandler.wmsCombineProcessingConfirm(QtFastjsonUtil.parse(duplicatedRecordEntity.getData(), ProcessOrderNoticeDTO.class));
                    break;
                case WMS_STOCK_CHECK:
                    stockHandler.wmsStockCheck(QtFastjsonUtil.parse(duplicatedRecordEntity.getData(), StockCountNoticeDTO.class));
                    break;
                case WMS_STOCK_TRANSFER:
                    stockHandler.wmsStockTransfer(QtFastjsonUtil.parse(duplicatedRecordEntity.getData(), TransferOrderNoticeDTO.class));
                    break;
                default:
                    break;
            }
        }
        log.info("库存管理-补偿任务-结束，此次待补偿记录数：{}，补偿成功记录数：{}", duplicatedRecordEntityList.size(), success);
    }
}
